﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Model;
using System.IO;
namespace LaKuaShop_TestDemo.Page
{
    /// <summary>
    /// ProductShowPage.xaml 的交互逻辑
    /// </summary>
    public partial class ProductShowPage : System.Windows.Controls.Page
    {
        Button[] buttons_id;
        TextBlock[] textBlocks;
        int rowcount = 5;
        public ProductShowPage()
        {
            InitializeComponent();
            ShowProduct();
        }
        
        //显示搜索结果
        private void ShowSearchProduct(string str)
        {
            //获取所有商品信息
            List<ProductInfo> products = new List<ProductInfo>();
            BLL.Product Product = new BLL.Product();
            products = Product.SearchProduct(str);
            List<byte[]> img = new List<byte[]>();
            for (int i = 0; i < products.Count; i++)
            {
                img.Add(Product.ShowProductImg(products[i].Pid));
            }
            buttons_id = new Button[products.Count];
            textBlocks = new TextBlock[products.Count];
            ProRefresh(products, img);
        }

        //添加商品到首页
        private void ShowProduct()
        {
            //获取所有商品信息
            List<ProductInfo> products = new List<ProductInfo>();
            BLL.Product Product = new BLL.Product();
            products = Product.ShowProductList();
            List<byte[]> img = new List<byte[]>();
            for (int i = 0; i < products.Count; i++)
            {
                img.Add(Product.ShowProductImg(products[i].Pid));
            }
            buttons_id = new Button[products.Count];
            textBlocks = new TextBlock[products.Count];
            ProRefresh(products, img);
        }

        //商品刷新
        private void ProRefresh(List<ProductInfo> products,List<byte[]> img)
        {
            Grid.Children.Clear();
            for (int i = 0; i < products.Count; i++)
            {
                buttons_id[i] = new Button();
                textBlocks[i] = new TextBlock();
                //textBlock布局大小设置
                textBlocks[i].Width = 120; textBlocks[i].Height = 120;
                textBlocks[i].HorizontalAlignment = HorizontalAlignment.Left; textBlocks[i].VerticalAlignment = VerticalAlignment.Top;
                textBlocks[i].Margin = new Thickness((i % rowcount) * 130, (i / rowcount) * 130, 0, 0);

                if (img[i] != null)//每个格子加上图片背景
                {
                    MemoryStream stream = new MemoryStream(img[i]);
                    BitmapImage bmp = new BitmapImage();
                    bmp.BeginInit();//初始化
                    bmp.StreamSource = stream;//设置源
                    bmp.EndInit();//初始化结束
                    buttons_id[i].Background = new ImageBrush(bmp);
                }
                buttons_id[i].Height = 100;
                buttons_id[i].Width = 120;

                // buttons_id[i].Background = Brushes.Transparent;
                buttons_id[i].Content = products[i].Pid;
                buttons_id[i].Foreground = Brushes.Transparent;

                buttons_id[i].Click += new RoutedEventHandler(Button_Click);
                textBlocks[i].Inlines.Add(buttons_id[i]);
                //textBlocks[i].Text = products[i].Pname;
                Grid.Children.Add(textBlocks[i]);

            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button thisButton = sender as Button;
            NavigationService.GetNavigationService(this).Navigate(new ProductDetailPage(thisButton.Content.ToString()));
            
        }

        //点击确认按钮
        private void Arg_Button(object sender, RoutedEventArgs e)
        {
            string order = OrderChoose.Text;
            string tag = TagChoose.Text;
            List<ProductInfo> pro_list = new List<ProductInfo>();
            List<byte[]> imgs = new List<byte[]>();
            BLL.Product Pro = new BLL.Product();
            if (order == "" && tag == "")
            {
                ShowProduct();
            }
            else if (order != "" && tag == "")
            {
                string method = (order == "价格升序") ? "desc" : "asc";
                pro_list = Pro.ShowProductList();
                pro_list = Pro.ProductOrder(method, pro_list);
                for (int i = 0; i < pro_list.Count; i++)
                {
                    imgs.Add(Pro.ShowProductImg(pro_list[i].Pid));
                }
                ProRefresh(pro_list, imgs);
            }
            else if (tag != "" && order == "")
            {
                pro_list = Pro.ShowProListByTag(tag);
                for (int i = 0; i < pro_list.Count; i++)
                {
                    imgs.Add(Pro.ShowProductImg(pro_list[i].Pid));
                }
                ProRefresh(pro_list, imgs);
            }
            else
            {
                string method = (order == "价格升序") ? "desc" : "asc";
                pro_list = Pro.ShowProListByTag(tag);
                pro_list = Pro.ProductOrder(method, pro_list);
                for (int i = 0; i < pro_list.Count; i++)
                {
                    imgs.Add(Pro.ShowProductImg(pro_list[i].Pid));
                }
                ProRefresh(pro_list, imgs);
            }
        }
        
        //商品推荐按钮
        private void Rcmd_button(object sender, RoutedEventArgs e)
        {
            //获取所有商品信息
            List<ProductInfo> products = new List<ProductInfo>();
            BLL.Product Product = new BLL.Product();
            products = Product.RcmdProducts();
            List<byte[]> img = new List<byte[]>();
            for (int i = 0; i < products.Count; i++)
            {
                img.Add(Product.ShowProductImg(products[i].Pid));
            }
            buttons_id = new Button[products.Count];
            textBlocks = new TextBlock[products.Count];
            ProRefresh(products, img);
        }

        //显示全部商品
        private void AllPro_button(object sender, RoutedEventArgs e)
        {
            ShowProduct();
        }

        //新品推荐
        private void NewPro_button(object sender, RoutedEventArgs e)
        {
            //获取所有商品信息
            List<ProductInfo> products = new List<ProductInfo>();
            BLL.Product Product = new BLL.Product();
            products = Product.NewProducts(DateTime.Now);
            List<byte[]> img = new List<byte[]>();
            for (int i = 0; i < products.Count; i++)
            {
                img.Add(Product.ShowProductImg(products[i].Pid));
            }
            buttons_id = new Button[products.Count];
            textBlocks = new TextBlock[products.Count];
            ProRefresh(products, img);
        }

        //按下搜索键
        private void Search_btn(object sender, RoutedEventArgs e)
        {
            if (finding.Text == "") return;
            ShowSearchProduct(finding.Text);
        }
    }
}

       